

import os
import re
import time
import traceback
import requests
from db_interface import db_interface
from srf_log import logger
import requests
from lxml import etree
import pickle
import random
import datetime
import json




def plate_data_list_spider(page):
    url = 'http://push2.eastmoney.com/api/qt/clist/get?' \
          'pn={}&pz=50&po=1&np=1&ut=b2884a393a59ad64002292a3e90d46a5&fltt=2&invt=2&fid=f62&fs=m:90+t:3&stat=1&fields=f12,f14,f2,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f204,f205,f124&rt=53111192&cb=jQuery1830042128019499628744_1593335734570&_=1593335763705'.format(page)
    r = requests.get(url)
    try:
        data = json.loads(r.text[43:-2])['data']['diff']
        for item in data:
            code = item['f12']
            name = item['f14']
            count, mkv = get_plate_data(code)
            data = {
                'code': code,
                'name': name,
                'mkv': mkv,
                'count': count
            }
            db_interface.dongcai_data.insert_dict('plate_name_list', data)
            print('insert into plate_name_list: %s' % data)
            # exit(-1)
    except Exception:
        logger.error('except in plate_data_list_spider: %s' % traceback.format_exc())




def get_plate_data(code):
    api = 'http://push2.eastmoney.com/api/qt/stock/get?secid=90.{}&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f59,f152,f43,f169,f170,f46,f60,f44,f45,f168,f50,f47,f48,f49,f46,f169,f161,f117,f85,f47,f48,f163,f171,f113,f114,f115,f86,f117,f85&cb=jQuery1124046852348339402616_1593337732693&type=CT&cmd=BK08741&sty=FDPBPFB&st=z&js=((x))&token=4f1862fc3b5e77c150a2b985b12db0fd&_=1593337732694'.format(code)
    try:
        r = requests.get(api)
        data = json.loads(r.text[43:-2])['data']
        rise = data['f113']
        down = data['f114']
        count = rise + down
        mkv = round(data['f117']/100000000, 2)
        return count, mkv
    except Exception:
        logger.error('except in get_plate_count: %s' % traceback.format_exc())




def plate_stock_data(page, plt_code, plt_name):
    url = 'http://push2.eastmoney.com/api/qt/clist/get?' \
          'pn={}&pz=50&po=1&np=1&ut=b2884a393a59ad64002292a3e90d46a5&fltt=2&invt=2&fid=f62&fs=b:{}&stat=1&fields=f12,f14,f2,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f204,f205,f124&rt=53111293&cb=jQuery18309340118285119199_1593338815927&_=1593338817443'.format(page, plt_code)
    r = requests.get(url)
    try:
        result = []
        data = json.loads(r.text[41:-2])['data']['diff']
        pe_dict = stock_pe_dict()
        for item in data:
            stock = item['f12']
            name = item['f14']
            type = 1 if stock.startswith('6') else 0
            pe = pb = ''
            if stock in pe_dict:
                pe, pb = pe_dict[stock]
            data_dict = {
                'plate': plt_code,
                'plate_name': plt_name,
                'stock': stock,
                'type': type,
                'name': name,
                'pe': pe,
                'pb': pb
            }
            result.append(data_dict)
        db_interface.dongcai_data.insert_bulkdata(result, 'plate_stock_list')
        print('insert into plate_stock_data: %s' % result)
    except Exception:
        logger.error('except in plate_data_list_spider: %s' % traceback.format_exc())



def stock_pe_dict():
    data = {}
    sql = "select stock, pe, pb from ids_stock_list"
    query = db_interface.dongcai_data.select(sql)
    for item in query:
        code = item[0]
        if code not in data:
            data[code] = item[1:]
    return data


def update_plt_stock():
    sql = "select code, name from plate_name_list where id > 178"
    query = db_interface.dongcai_data.select(sql)
    for item in query:
        code, name = item
        for page in range(1, 10):
            plate_stock_data(page, code, name)



if __name__ == "__main__":
    # for page in range(1, 7):
    #     plate_data_list_spider(page)
    # plate_stock_data(1, 'BK0874')
    update_plt_stock()